﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Input;
using ApiScanner.Core;

namespace ApiScannerShell.ViewModels
{
    public sealed class FileSystemScannerModuleConfigurationElementCollectionViewModelCollection: ViewModelCollection<IViewModel<FileSystemScannerModuleConfigurationElement>, FileSystemScannerModuleConfigurationElement>
    {
        public ICommand AddExchangeDllDependencyScannerCommand { get; private set; }
        public ICommand AddExchangeDllScannerCommand { get; private set; }

        public FileSystemScannerModuleConfigurationElementCollectionViewModelCollection(ICollection<FileSystemScannerModuleConfigurationElement> modelItems) : base(modelItems)
        {
            AddExchangeDllScannerCommand = new DelegateCommand(() => !this.OfType<ExchangeDllScannerTestConfigurationElementViewModel>().Any(), () => AddItem(new ExchangeDllScannerTestConfigurationElementViewModel(new ExchangeDllScannerTestConfigurationElement())));
            AddExchangeDllDependencyScannerCommand = new DelegateCommand(() => !this.OfType<ExchangeDllDependencyScannerTestConfigurationElementViewModel>().Any(), () => AddItem(new ExchangeDllDependencyScannerTestConfigurationElementViewModel(new ExchangeDllDependencyScannerConfigurationElement())));

            AddValidationRule(() => !this.Any() ? "At least one scanner must be specified." : null);
        }

        protected override IViewModel<FileSystemScannerModuleConfigurationElement> CreateViewModel(FileSystemScannerModuleConfigurationElement model)
        {
            var exchangeScanner = model as ExchangeDllScannerTestConfigurationElement;
            var dependencyScanner = model as ExchangeDllDependencyScannerConfigurationElement;
            if (exchangeScanner != null)
            {
                return new ExchangeDllScannerTestConfigurationElementViewModel(exchangeScanner);
            }
            if (dependencyScanner != null)
            {
                return new ExchangeDllDependencyScannerTestConfigurationElementViewModel(dependencyScanner);
            }

            throw new NotSupportedException(string.Format("The model type {0} is not supported.", model.GetType().FullName));
        }
    }
}